

*set filepath. NOTE: you will need to update the filepath to run this file
cd "/Users/jeremyhorowitz/Documents/Book/Replication files/Chapter 2"



***********************************************************************************
* Table 2.1 **********************************************************************
***********************************************************************************

*load data: 2012-2013 panel survey
use "panel2012.dta", clear

recode q703_r1 (2=1) (1=0) (777/999=0) (.=.), gen(kenyatta_fav)
ttest kenyatta_fav, by(kikuyu_r1)

recode q704_r1 (2=1) (1=0) (777/999=0) (.=.), gen(moi_fav)
ttest moi_fav, by(kalenjin_r1)

recode q705_r1 (2=1) (1=0) (777/999=0) (.=.), gen(kibaki1_fav)
ttest kibaki1_fav, by(kikuyu_r1)

recode q706_r1 (2=1) (1=0) (777/999=0) (.=.), gen(kibaki2_fav)
ttest kibaki2_fav, by(kikuyu_r1)


***********************************************************************************
* Figure 2.1 **********************************************************************
***********************************************************************************

*load data: 2012-2013 panel survey
use "panel2012.dta", clear

*create sample identifiers
gen sample=""
replace sample="Kikuyus" if kikuyu_r1==1
replace sample="Luos" if luo_r1==1

gen sample2=""
replace sample2="Jubilee-aligned groups" if jub_groups==1
replace sample2="CORD-aligned groups" if cord_groups==1

*generate means
preserve
collapse jub_inc cord_inc uhuru_rep odinga_rep uhuru_funds odinga_funds, by(sample)
save aggregates.dta,replace
restore

collapse jub_inc cord_inc uhuru_rep odinga_rep uhuru_funds odinga_funds, by(sample2)
append using aggregates.dta

*organization
replace sample2=sample if sample!=""
drop if sample2==""
drop sample
rename sample2 sample
gen groupnum=. in 1/4
replace groupnum=1 if sample=="Kikuyus"
replace groupnum=2 if sample=="Jubilee-aligned groups"
replace groupnum=3 if sample=="Luos"
replace groupnum=4 if sample=="CORD-aligned groups"
sort groupnum

*replace as percentages
replace jub_inc=jub_inc*100
replace cord_inc=cord_inc*100
replace uhuru_rep=uhuru_rep*100
replace odinga_rep=odinga_rep*100
replace uhuru_funds=uhuru_funds*100
replace odinga_funds=odinga_funds*100

*graph - top panel
graph bar jub_inc , over(groupnum, relabel(1 "Kikuyus" 2 `""Jubilee" "coalition""' 3 "Luos"  4 `""CORD" "coalition""') ) ///
ytitle("") blabel(total, format(%8.0f)) title("Jubilee will serve the interests of all groups") ///
bar(1, fcolor(gs4) color(black)) bar(2, fcolor(gs12) color(black)) ///
scheme(s1mono) plotregion(lcolor(white)) ylabel(, angle(0)) yscale(noextend) ylab(0(20)100, nogrid) scale(1.5) xsize(9) aspectratio(.25) name(g1, replace)

graph bar cord_inc , over(groupnum, relabel(1 "Kikuyus" 2 `""Jubilee" "coalition""' 3 "Luos"  4 `""CORD" "coalition""') ) ///
ytitle("") blabel(total, format(%8.0f)) title("CORD will serve the interests of all groups") ///
bar(1, fcolor(gs12) color(black)) ///
scheme(s1mono) plotregion(lcolor(white)) ylabel(, angle(0)) yscale(noextend) ylab(0(20)100, nogrid) scale(1.5) xsize(9) aspectratio(.25) name(g2, replace)

graph combine g1 g2, row(1) scheme(s1mono) xsize(9) ysize(2.5) iscale(1.05)

*graph - middle panel
graph bar uhuru_rep , over(groupnum, relabel(1 "Kikuyus" 2 `""Jubilee" "coalition""' 3 "Luos"  4 `""CORD" "coalition""') ) ///
ytitle("") blabel(total, format(%8.0f)) title("Kenyatta will represent the interests of R's group" "somewhat or very well") ///
bar(1, fcolor(gs4) color(black)) bar(2, fcolor(gs12) color(black)) ///
scheme(s1mono) plotregion(lcolor(white)) ylabel(, angle(0)) yscale(noextend) ylab(0(20)100, nogrid) scale(1.5) xsize(9) aspectratio(.25) name(g1, replace)

graph bar odinga_rep , over(groupnum, relabel(1 "Kikuyus" 2 `""Jubilee" "coalition""' 3 "Luos"  4 `""CORD" "coalition""') ) ///
ytitle("") blabel(total, format(%8.0f)) title("Odinga will represent the interests of R's group" "somewhat or very well") ///
bar(1, fcolor(gs12) color(black)) ///
scheme(s1mono) plotregion(lcolor(white)) ylabel(, angle(0)) yscale(noextend) ylab(0(20)100, nogrid) scale(1.5) xsize(9) aspectratio(.25) name(g2, replace)

graph combine g1 g2, row(1) scheme(s1mono) xsize(9) ysize(2.5) iscale(1.05)

*graph - bottom panel
graph bar uhuru_funds , over(groupnum, relabel(1 "Kikuyus" 2 `""Jubilee" "coalition""' 3 "Luos"  4 `""CORD" "coalition""') ) ///
ytitle("") blabel(total, format(%8.0f)) title("Area will receive some / a lot of funds" "if Kenyatta is elected") ///
bar(1, fcolor(gs4) color(black)) bar(2, fcolor(gs12) color(black)) ///
scheme(s1mono) plotregion(lcolor(white)) ylabel(, angle(0)) yscale(noextend) ylab(0(20)100, nogrid) scale(1.5) xsize(9) aspectratio(.25) name(g1, replace)

graph bar odinga_funds , over(groupnum, relabel(1 "Kikuyus" 2 `""Jubilee" "coalition""' 3 "Luos"  4 `""CORD" "coalition""') ) ///
ytitle("") blabel(total, format(%8.0f)) title("Area will receive some / a lot of funds" "if Odinga is elected") ///
bar(1, fcolor(gs12) color(black)) ///
scheme(s1mono) plotregion(lcolor(white)) ylabel(, angle(0)) yscale(noextend) ylab(0(20)100, nogrid) scale(1.5) xsize(9) aspectratio(.25) name(g2, replace)

graph combine g1 g2, row(1) scheme(s1mono) xsize(9) ysize(2.5) iscale(1.05)






********************************************************************************************
********************* Figure 2.2 ***********************************************************
********************************************************************************************

use "survey2015.dta", clear

***TOP PANEl: PREFERENCES

*estimate preferences for pooled sample and by ethnic group
reg win i.coeth if eg_con==1, cluster(respelec)
margins coeth, saving(j1, replace)
reg win i.coeth if eg_con==1 & eth=="kikuyu", cluster(respelec)
margins coeth, saving(j2, replace)
reg win i.coeth if eg_con==1 & eth=="luo", cluster(respelec)
margins coeth, saving(j3, replace)
reg win i.coeth if eg_con==1 & eth=="luhya", cluster(respelec)
margins coeth, saving(j4, replace)
reg win i.coeth if eg_con==1 & eth=="kamba", cluster(respelec)
margins coeth, saving(j5, replace)
reg win i.coeth if eg_con==1 & eth=="kisii", cluster(respelec)
margins coeth, saving(j6, replace)

preserve 

use j1, clear
append using j2
append using j3 
append using j4
append using j5
append using j6

egen can=seq(), from(0) to(1)
gen xplace=_n
gen order=_n

*set xplacement
replace xplace=.90 if order==1
replace xplace=1.10 if order==2
replace xplace=1.9 if order==3
replace xplace=2.1 if order==4
replace xplace=2.9 if order==5
replace xplace=3.1 if order==6
replace xplace=3.9 if order==7
replace xplace=4.1 if order==8
replace xplace=4.9 if order==9
replace xplace=5.1 if order==10
replace xplace=5.9 if order==11
replace xplace=6.1 if order==12

format _margin %8.2f 

scatter _margin xplace if can==0, mcolor(black) msymbol(O) msize(medium) mlabel(_margin) mlabposition(9) ||  ///
rspike _ci_lb _ci_ub xplace if can==0, color(black) || ///
scatter _margin xplace if can==1, mcolor(red) msymbol(Oh) msize(medium) mlabel(_margin) mlabposition(3)  mlabgap(*2) ||  ///
rspike _ci_lb _ci_ub xplace if can==1, ///
color(red) xscale(range(0.5 6.55)) yscale(range(0 1)) scheme(s1mono) ///
xlabel(1 `""Full" "Sample""' 2 "Kikuyus" 3 "Luos" 4 "Luhyas" 5 "Kambas" 6 "Kisiis") ///
xtitle(" ") ylabel(0 "0" .2 ".2" .4 ".4" .6 ".6" .8 ".8" 1 "1", angle(0)) ///
legend(order(1 3) pos(6) col(2) label(1 "Non-coethnic") label(3 "Coethnic")) ///
plotregion(lcolor(white)) ytitle("Pr(vote)") xline(1.5 2.5 3.5 4.5 5.5, lp(dash)) ///
plotregion(margin(zero)) xsize(8) aspectratio(.25) scale(1.1) title("(a) Preferences")

restore


***TOP PANEl: REPRESENTATION BELIEFS

*estimate beliefs for pooled sample and by ethnic group
reg rep i.coeth if eg_con==1, cluster(respelec)
margins coeth, saving(j1, replace)
reg rep i.coeth if eg_con==1 & eth=="kikuyu", cluster(respelec)
margins coeth, saving(j2, replace)
reg rep i.coeth if eg_con==1 & eth=="luo", cluster(respelec)
margins coeth, saving(j3, replace)
reg rep i.coeth if eg_con==1 & eth=="luhya", cluster(respelec)
margins coeth, saving(j4, replace)
reg rep i.coeth if eg_con==1 & eth=="kamba", cluster(respelec)
margins coeth, saving(j5, replace)
reg rep i.coeth if eg_con==1 & eth=="kisii", cluster(respelec)
margins coeth, saving(j6, replace)

use j1, clear
append using j2
append using j3 
append using j4
append using j5
append using j6

egen can=seq(), from(0) to(1)
gen xplace=_n
gen order=_n

*set xplacement
replace xplace=.90 if order==1
replace xplace=1.10 if order==2
replace xplace=1.9 if order==3
replace xplace=2.1 if order==4
replace xplace=2.9 if order==5
replace xplace=3.1 if order==6
replace xplace=3.9 if order==7
replace xplace=4.1 if order==8
replace xplace=4.9 if order==9
replace xplace=5.1 if order==10
replace xplace=5.9 if order==11
replace xplace=6.1 if order==12

format _margin %8.1f 

scatter _margin xplace if can==0, mcolor(black) msymbol(O) msize(medium) mlabel(_margin) mlabposition(9) ||  ///
rspike _ci_lb _ci_ub xplace if can==0, color(black) || ///
scatter _margin xplace if can==1, mcolor(red) msymbol(Oh) msize(medium) mlabel(_margin) mlabposition(3)  mlabgap(*2) ||  ///
rspike _ci_lb _ci_ub xplace if can==1, ///
color(red) xscale(range(0.5 6.55)) yscale(range(1 7)) scheme(s1mono) ///
xlabel(1 `""Full" "Sample""' 2 "Kikuyus" 3 "Luos" 4 "Luhyas" 5 "Kambas" 6 "Kisiis") ///
xtitle(" ") ylabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7", angle(0)) ///
legend(order(1 3) pos(6) col(2) label(1 "Non-coethnic") label(3 "Coethnic")) ///
plotregion(lcolor(white)) ytitle("Mean Rep. Score") xline(1.5 2.5 3.5 4.5 5.5, lp(dash)) ///
plotregion(margin(zero)) xsize(8) aspectratio(.25) scale(1.1) title("(b) Representation Beliefs")




********************************************************************************************
********************* Figure 2.3 ***********************************************************
********************************************************************************************

use "Kibaki voteshare over time.dta", clear

twoway (scatter Kikuyu year, msize(small)) (line Kikuyu year), legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2008)) ytitle("") xtitle("") title("Kikuyu") name(g1, replace)

twoway (scatter Luhya year, msize(small)) (line Luhya year), legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2008)) ytitle("") xtitle("") title("Luhya") name(g2, replace)

twoway (scatter Luo year, msize(small)) (line Luo year), legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2008)) ytitle("") xtitle("") title("Luo") name(g3, replace)

twoway (scatter Kalenjin year, msize(small)) (line Kalenjin year), legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2008)) ytitle("") xtitle("") title("Kalenjin") name(g4, replace)

twoway (scatter Kamba year, msize(small)) (line Kamba year), legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2008)) ytitle("") xtitle("") title("Kamba") name(g5, replace)

twoway (scatter Kisii year, msize(small)) (line Kisii year), legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2008)) ytitle("") xtitle("") title("Kisii") name(g6, replace)

twoway (scatter Meru_Embu year, msize(small)) (line Meru_Embu year), legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2008)) ytitle("") xtitle("") title("Meru/Embu") name(g7, replace)

twoway (scatter Mijikenda year, msize(small)) (line Mijikenda year), legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2008)) ytitle("") xtitle("") title("Mijikenda") name(g8, replace)

graph combine g1 g7 g2 g3 g5 g6 g8 g4, scheme(s1mono) cols(2) ysize(6) iscale(*.8)



********************************************************************************************
********************* Figure 2.4 ***********************************************************
********************************************************************************************

use "Bloc vote over time.dta", clear

twoway (scatter bv_rate year, msize(small)) (line bv_rate year) if group=="Kikuyu", legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007" 2012 "2012" 2017 "2017") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2018)) ytitle("") xtitle("") title("Kikuyu") name(g1, replace)

twoway (scatter bv_rate year, msize(small)) (line bv_rate year) if group=="Luhya", legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007" 2012 "2012" 2017 "2017") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2018)) ytitle("") xtitle("") title("Luhya") name(g2, replace)

twoway (scatter bv_rate year, msize(small)) (line bv_rate year) if group=="Luo", legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007" 2012 "2012" 2017 "2017") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2018)) ytitle("") xtitle("") title("Luo") name(g3, replace)

twoway (scatter bv_rate year, msize(small)) (line bv_rate year) if group=="Kalenjin", legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007" 2012 "2012" 2017 "2017") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2018)) ytitle("") xtitle("") title("Kalenjin") name(g4, replace)

twoway (scatter bv_rate year, msize(small)) (line bv_rate year) if group=="Kamba", legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007" 2012 "2012" 2017 "2017") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2018)) ytitle("") xtitle("") title("Kamba") name(g5, replace)

twoway (scatter bv_rate year, msize(small)) (line bv_rate year) if group=="Kisii", legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007" 2012 "2012" 2017 "2017") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2018)) ytitle("") xtitle("") title("Kisii") name(g6, replace)

twoway (scatter bv_rate year, msize(small)) (line bv_rate year) if group=="Meru", legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007" 2012 "2012" 2017 "2017") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2018)) ytitle("") xtitle("") title("Meru") name(g7, replace)

twoway (scatter bv_rate year, msize(small)) (line bv_rate year) if group=="Kalenjin", legend(off) scheme(s1mono) ///
ylabel(0(25)100, angle(0)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007" 2012 "2012" 2017 "2017") ///
plotregion(lcolor(white)) yscale(noextend) xscale(range(1996 2018)) ytitle("") xtitle("") title("Mijikenda") name(g8, replace)

graph combine g1 g2 g3 g4 g5 g6 g7 g8, scheme(s1mono) cols(2) ysize(6) iscale(*.8)
